import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2024-08-04
 * Time: 18:03
 */
class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;

    public MyStack() {
        queue1=new LinkedList<>();
        queue2=new LinkedList<>();
    }

    public void push(int x) {
        if(empty()){
            queue1.offer(x);
        }else if(queue1.isEmpty()){
            queue2.offer(x);
        }else{
            queue1.offer(x);
        }
    }

    public int pop() {
        if (queue1.isEmpty()) {
            int size = queue2.size();
            for (int i = 0; i < size - 1; i++) {
                queue1.offer(queue2.poll());
            }
            return queue2.poll();
        } else {
            int size = queue1.size();
            for (int i = 0; i < size - 1; i++) {
                queue2.offer(queue1.poll());
            }
            return queue1.poll();
        }
    }

    public int top() {
        int val=0;
        if (queue1.isEmpty()) {
            int size = queue2.size();
            for (int i = 0; i < size; i++) {
                val=queue2.poll();
                queue1.offer(val);
            }
            return val;
        } else {
            int size = queue1.size();
            for (int i = 0; i < size; i++) {
                val=queue1.poll();
                queue2.offer(val);
            }
            return val;
        }
    }

    public boolean empty() {
        return queue1.isEmpty()&&queue2.isEmpty();
    }
}
